import java.io.*;
import com.alibaba.fastjson.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Result {
    // 输出决赛对应运动项目结果
    // 输入：比赛项目名称
    // 输出：比赛项目结果
    public static void showOffResult(String string) {
        String json = "src/data/" + string + ".json";
        File jsonFile = new File(json);
        String jsonData;
        jsonData = IO.getStr(jsonFile);

        JSONObject object = JSONObject.parseObject(jsonData);
        // 获取list,包括Final,Semifinal和Preliminary
        List<HashMap> list = JSON.parseArray((object.get("Heats")).toString(), HashMap.class);
        // 获取results,包括FullName,Rank，TotalPoints和Dives(各个评委得分）
        // todo:detail只需要把它改成遍历list,list.get(0 / 1 / 2)
        List<HashMap> results = JSON.parseArray(list.get(0).get("Results").toString(), HashMap.class);

        for (HashMap hashMap : results) {
            //判断是否是双人项目
            boolean pairEvent = string.startsWith("synchronised",string.length()-"synchronised".length());
            if(!pairEvent){
                System.out.print("FullName:");
                System.out.println(hashMap.get("FullName"));
            }
            else {
                List<String> competitorsName = new ArrayList<>();
                // 获取两个选手的名字
                List<HashMap> competitors = JSON.parseArray(hashMap.get("Competitors").toString(), HashMap.class);
                for (HashMap competitor : competitors) {
                    competitorsName.add(competitor.get("FullName").toString());
                }
                competitorsName.sort(null);

                System.out.print("FullName:");
                System.out.println(competitorsName.get(0) + " & " + competitorsName.get(1));
            }

            System.out.print("Rank:");
            System.out.println(hashMap.get("Rank"));

            System.out.print("Score:");
            // 获取Dives:各个评委得分
            List<HashMap> dives = JSON.parseArray((hashMap.get("Dives")).toString(), HashMap.class);
            int lastIndex = dives.size();
            //输出各个评委得分
            for (HashMap dive : dives) {
                System.out.print(dive.get("DivePoints"));
                if (--lastIndex != 0){
                    System.out.print(" + ");
                }
                else {
                    System.out.print(" = ");
                }
            }
            System.out.print(hashMap.get("TotalPoints").toString());

            System.out.println("\n-----");
        }
    }
}
